package com.building.hcrm.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.building.hcrm.R;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseAsyncTask;
import com.building.hcrm.common.ComFunc;
import com.building.hcrm.common.User;
import com.building.hcrm.wcf.UserServiceProxy;

public class ChangePwdActivity extends BaseActivity
{
	private EditText txtOldPassword;
	private EditText txtNewPassword;
	private EditText txtMakeSurePassword;
	private Button btnCancel;
	private Button imgbtnMakeSure;

	private ImageButton _imgBtnRe;
	private TextView _txtTitle;
	private ImageButton _imgBtnTitleRight;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		InitTitle();
		init();
	}

	private void InitTitle()
	{
		_imgBtnRe = (ImageButton) findViewById(R.id.imgbtn_home);
		_imgBtnRe.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO 自动生成的方法存根
				finish();
			}
		});
		_txtTitle = (TextView) findViewById(R.id.titlePageName);
		_txtTitle.setText(R.string.per_change_password);
		_imgBtnTitleRight = (ImageButton) findViewById(R.id.imgbtn_save);
		_imgBtnTitleRight.setVisibility(View.INVISIBLE);
	}

	private void init()
	{
		txtOldPassword = (EditText) findViewById(R.id.txtOldPassword);
		txtNewPassword = (EditText) findViewById(R.id.txtnewPassword);
		txtMakeSurePassword = (EditText) findViewById(R.id.txtmakeSurePassword);
		btnCancel = (Button) findViewById(R.id.imgbtn_cancel);
		btnCancel.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO 自动生成的方法存根
				finish();
			}
		});
		imgbtnMakeSure = (Button) findViewById(R.id.imgbtn_makesure);
		imgbtnMakeSure.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO 自动生成的方法存根
				if (txtOldPassword.getText().length() == 0)
				{
					Toast.makeText(ChangePwdActivity.this, "请输入旧密码！", Toast.LENGTH_SHORT).show();
					return;
				}
				if (txtNewPassword.getText().length() == 0)
				{
					Toast.makeText(ChangePwdActivity.this, "请输入新密码！", Toast.LENGTH_SHORT).show();
					return;
				}
				if (txtNewPassword.getText().toString().equals(txtMakeSurePassword.getText().toString()) == false)
				{
					Toast.makeText(ChangePwdActivity.this, "两次密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
					txtNewPassword.setText("");
					txtMakeSurePassword.setText("");
					return;
				}

				new BaseAsyncTask<String, String, String>(ChangePwdActivity.this)
				{

					@Override
					protected void onPreExecute()
					{
						super.onPreExecute();
					}

					@Override
					protected String doInBackground(String... params)
					{
						String Re = null;
						if (null != params && params.length > 2)
						{
							try
							{
								Re = UserServiceProxy.getInstance(getApplicationContext()).UserReset(params[0],
										params[1], params[2]);
							}
							catch (Exception e)
							{
								Log.e(LTAG, ComFunc.getExceptionMessage(e), e);
							}
						}
						return Re;
					}

					@Override
					protected void onPostExecute(String result)
					{
						super.onPostExecute(result);
						if("success".equals(result))
						{
							Toast.makeText(ChangePwdActivity.this,"修改成功！",Toast.LENGTH_SHORT).show();
							finish();
						}
						else if("nochange".equals(result))
						{
							Toast.makeText(ChangePwdActivity.this,"密码未变更，请核对！",Toast.LENGTH_SHORT).show();
						}
						else if("unpass".equals(result))
						{
							Toast.makeText(ChangePwdActivity.this,"旧密码错误，请核对！",Toast.LENGTH_SHORT).show();
						}
						else if("failed".equals(result))
						{
							Toast.makeText(ChangePwdActivity.this,"修改失败，请核对！",Toast.LENGTH_SHORT).show();
						}
						else
						{
							Toast.makeText(ChangePwdActivity.this,"修改失败，请核对！",Toast.LENGTH_SHORT).show();
						}
					}
				}.execute(User.getInstance(ChangePwdActivity.this).getUserNum(), txtOldPassword.getText().toString(),
						txtNewPassword.getText().toString());
			}
		});
	}
}
